'use strict'

angular.module('mainApp')
    .service('tjzHttp', ['$http', '$q',function($http,$q) {
        var postRequest = function(url,data){
            var defer = $q.defer();
            $http({
                method: 'POST',
                url: url,
                headers:{
                    'Content-Type': 'application/json',
                },
                data: JSON.stringify(data)
            }).then(function(data) {
                defer.resolve(data);
            },function(response){
                defer.reject(response);
            });
            return defer.promise;
        };

        var postFullRequest = function(url,data){
            var defer = $q.defer();
            $http({
                method: 'GET',
                url: url,
                headers:{
                    'Content-Type': 'application/json',
                },
                data: JSON.stringify(data)
            }).then(function(data) {
                defer.resolve(data);
            },function(response){
                defer.reject(response);
            });
            return defer.promise;
        };

        var postFormRequest = function(url,data){
            var defer = $q.defer();
            $http({
                method: 'POST',
                url: url,
                headers:{
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: $.param(data)
            }).then(function(data) {
                defer.resolve(data);
            },function(response){
                defer.reject(response);
            });
            return defer.promise;
        };

        var getRequest = function(url){
            var defer = $q.defer();
            $http({
                method: 'GET',
                url: url
            }).then(function(data) {
                defer.resolve(data);
            },function(response){
                defer.reject(response);
            });
            return defer.promise;
        };

        var postUrlRequest = function(url){
            var defer = $q.defer();
            $http({
                method: 'POST',
                url: url
            }).then(function(data) {
                defer.resolve(data);
            },function(response){
                defer.reject(response);
            });
            return defer.promise;
        };

        var putRequest = function(url,data){
            var defer = $q.defer();
            $http({
                method: 'PUT',
                url: url,
                headers:{
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: data
            }).then(function(data) {
                defer.resolve(data);
            },function(response){
                defer.reject(response);
            });
            return defer.promise;
        };
        var deleteRequest = function(url){
            var defer = $q.defer();
            var url = url;
            $http({
                method: 'DELETE',
                url: url
            }).then(function(data) {
                defer.resolve(data);
            },function(response){
                defer.reject(response);
            });
            return defer.promise;
        };

        return {
            postRequest: postRequest,
            postFormRequest: postFormRequest,
            getRequest: getRequest,
            putRequest: putRequest,
            deleteRequest: deleteRequest,
            postFullRequest : postFullRequest,
            postUrlRequest: postUrlRequest
        };
    }]);



